﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using log4net;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using SysAdmin._00_Def;

namespace SysAdmin._03_Service._00_Db
{
    public class ShopUserService
    {
        public static IList<MyShopUserInfo> QueryAllShopUsers(int shopManagerUserId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<ShopUserEntity>()
                    .InnerJoin<UserInfoEntity>((su, u1) => su.UserId == u1.Id)
                    .InnerJoin<UserInfoEntity>((su, u1, u2) => u1.ReferenceInvitationCode == u2.InvitationCode)
                    .Where((su, u1, u2) => su.ShopManagerUserId == shopManagerUserId)
                    .Select((su, u1, u2) => new MyShopUserInfo
                    {
                        ShopUserInfo = su,
                        UserInfo = u1,
                        ReferenceUserInfo = u2
                    })
                    .ToList();
                return ls;
            }
        }

        public static bool DeleteShopUserById(int shopUserId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Deleteable<ShopUserEntity>(shopUserId).ExecuteCommand();
                return n > 0;
            }
        }

        public static bool DeleteShopUserByShopManagerUserId(int shopManagerUserId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Deleteable<ShopUserEntity>(x => x.ShopManagerUserId == shopManagerUserId)
                    .ExecuteCommand();
                return n > 0;
            }
        }

        public static bool Add(int shopManagerUserId, int userId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Queryable<ShopUserEntity>().Where(x => x.ShopManagerUserId == shopManagerUserId && x.UserId == userId).Count();
                if (n > 0)
                {
                    throw new MyException("该成员已在店铺中，请重新选择！");
                }
                else
                {
                    var su = new ShopUserEntity
                    {
                        ShopManagerUserId = shopManagerUserId,
                        UserId = userId
                    };
                    int n2 = db.Deleteable<ShopUserEntity>(x => x.UserId == userId).ExecuteCommand();
                    int n3 = db.Insertable(su).ExecuteCommand();
                    return n3 > 0;
                }
            }
        }

        public static bool UpdateShopUser(int shopManagerUserId
            , IList<int> shopUserIds
            , IList<int> noneShopUserIds)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                foreach (var item in shopUserIds)
                {
                    int n = db.Queryable<ShopUserEntity>()
                        .Where(x => x.ShopManagerUserId == shopManagerUserId
                            && x.UserId == item)
                        .Count();
                    if (n == 0)
                    {
                        var entity = new ShopUserEntity
                        {
                            UserId = item,
                            ShopManagerUserId = shopManagerUserId
                        };
                        db.Insertable<ShopUserEntity>(entity).ExecuteCommand();
                    }
                }
                if (noneShopUserIds.Count > 0)
                {
                    db.Deleteable<ShopUserEntity>(x => x.ShopManagerUserId == shopManagerUserId
                                            && noneShopUserIds.Contains(x.UserId))
                      .ExecuteCommand();
                }
                return true;
            }
        }
    }
}
